$(document).ready(function(){
	
	
	 if ($("#codigoRastreo").length > 0) {
		$("#codigoRastreo").keyup(function(){
			validarSoloNumeros($(this));
		});
		$("#codigoRastreo").change(function(){
			validarSoloNumeros($(this));
		});
	 }
	 
	 
	if ($("#inputEmail").length > 0) {
		 $("#inputEmail").change(function(){
			 validarEmail($(this));
		 });
	 }
	 
	
	if ($("#inputTelef").length > 0) {
		 $("#inputTelef").keyup(function(){
			 validarSoloNumeros($(this));
		 });
		 $("#inputTelef").change(function(){
			 validarSoloNumeros($(this));
		 });
	 }
	 
	
});

function validarSoloNumeros(data){
	var str = data.attr("value");
	
	for(var i = 0;i<str.length;i++){
		if(!(str[i] in { '1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1, '9': 1, '0': 1 })){
			var str2 = str.substr(0,i);
			data.attr("value",str2);
			i = str.length;
		}
	}
}


function validarEmail(data){	
	var valor = data.attr("value");
    
    var filter = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
    
    if(!filter.test(valor)){
    	data.attr("value","");
    }
}

function raiseError(msg) {
    this.errorList[this.errorList.length] = msg;
}

function numErrors() {
    return this.errorList.length;
}

function formValidator() {
    this.errorList = new Array;

    this.isEmpty = isEmpty;
    this.isNumber = isNumber;
    this.isAlphabetic = isAlphabetic;
    this.isAlphaNumeric = isAlphaNumeric;
    this.isWithinRange = isWithinRange;
    this.raiseError = raiseError;
    this.numErrors = numErrors; 
    this.displayErrors = displayErrors; 
}

function displayErrors() {
    for (x = 0; x < this.errorList.length; x++) {
        //alert("Error: " + this.errorList[x]);
    }
}

function isEmpty(val) {
    if (val.match(/^s+$/) || val == "") {
        return true;
    }
    else {
        return false;
    }
}


function isNumber(val) {
    if (val.match(/^[0-9]+$/)) {
        return true;
    }
    else {
        return false;
    }
}

function isAlphabetic(val) {
    if (val.match(/^[a-zA-ZáéíóúÁÉÍÓÚ ]+$/)) {
        return true;
    }
    else {
        return false;
    }
}

function isAlphaNumeric(val) {
    if (val.match(/^[a-zA-Z0-9-áéíóúÁÉÍÓÚ. ]+$/)) {
        return true;
    }
    else {
        return false;
    }
}

function isWithinRange(val, min, max) {
    if (val >= min && val <= max) {
        return true;
    }
    else {
        return false;
    }
}
